//
//  SuggestionViewController.m
//  QuickCure
//
//  Created by 周洋 on 16/9/12.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "BBZSuggestionViewController.h"
#import "Masonry.h"
#import "BBZTextView.h"
#import "SVProgressHUD.h"

@interface BBZSuggestionViewController () <UITableViewDelegate, UITableViewDataSource, UITextViewDelegate>

@property (nonatomic, weak) BBZTextView *textview;
//显示字数的label
@property (nonatomic, weak) UILabel *numberLabel;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation BBZSuggestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置初始化界面
    [self setupUI];
}


#pragma mark -- 初始化界面
- (void)setupUI {
    
    //输入框textView
    BBZTextView *textview = [[BBZTextView alloc] init];

    [self.view addSubview:textview];
    
    self.textview = textview;
    
    self.textview.delegate = self;
    
    [textview setFont:kFONT14];
    
    [textview setBackgroundColor:[UIColor colorWithRed:0.922 green:0.917 blue:0.922 alpha:1.000]];
    
    textview.tLabel.text = @"请输反馈信息";
    
    [textview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.leading.equalTo(self.view).offset(12);
        make.trailing.equalTo(self.view).offset(-12);
        make.height.equalTo(@180);
    }];
    

    //显示文字的label
    UILabel *tLabel = [[UILabel alloc] init];
    
    [self.view addSubview:tLabel];
    
    [tLabel setFont:kFONT16];
    //适应文字内容
    [tLabel sizeToFit];
    
    [tLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.textview.bottom).offset(15);
        make.leading.equalTo(self.textview).offset(30);
    }];
    
    tLabel.text = @"您还可以输入的字数:";

    //显示字数的label
    UILabel *numberLabel = [[UILabel alloc] init];
    
    [self.view addSubview:numberLabel];
    
    self.numberLabel = numberLabel;
    
    numberLabel.text = @"200";
    
    [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(tLabel.trailing).offset(5);
        make.centerY.equalTo(tLabel);
        make.width.equalTo(@33);
    }];
    
    //创建提交button
    UIButton *submitBtn = [[UIButton alloc] init];
    
    [self.view addSubview:submitBtn];
    
    [submitBtn setBackgroundColor:[UIColor colorWithRed:0.053 green:0.755 blue:0.770 alpha:1.000]];
    
    [submitBtn setTitle:@"提交反馈" forState:UIControlStateNormal];
    
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [submitBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    
    submitBtn.layer.cornerRadius = 5;
    
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(numberLabel);
        make.trailing.equalTo(self.textview);
        make.width.equalTo(@73);
    }];
    
    [submitBtn addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside];
    
    //添加tableView
    [self setupTableView];
}

#pragma mark -- UITextView代理方法
- (void)textViewDidChange:(UITextView *)textView {
    
    //获取textView的内容及文字长度
    NSString *text = self.textview.text;
    NSInteger textLength = text.length;
    if(textLength != 0) {
        
        self.textview.tLabel.hidden = YES;
    }
    else {
        
        self.textview.tLabel.hidden = NO;
    }
    
    //改变numberLabel显示的字数
    if(textLength <= 200) {
        
        textLength = 200 - textLength;
        self.numberLabel.text = [NSString stringWithFormat:@"%ld", textLength];
        self.numberLabel.textColor = [UIColor blackColor];
    }
    else {
        
        textLength = 0;
        self.numberLabel.text = [NSString stringWithFormat:@"%ld", textLength];
        self.numberLabel.textColor = [UIColor redColor];
    }
}

#pragma mark -- 提交button的点击事件
- (void)submitClick {
    
    //根据textView内容的长度判断
    NSInteger number = self.textview.text.length;
    
    if(number == 0) {
        
        //显示弹窗，提示：输入反馈信息
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showInfoWithStatus:@"请输入反馈信息" ];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
        });
    }
    else if(number > 200) {
        
        //提示错误：不能超过200字
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showErrorWithStatus:@"字数不能超过200" ];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
        });
    }
    else {
        
        //显示提交成功，回到上一个界面
        [self.navigationController popViewControllerAnimated:NO];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
            [SVProgressHUD showSuccessWithStatus:@"返回成功" ];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [SVProgressHUD dismiss];
            });
        });
    }
}


#pragma mark -- 添加tableview
- (void)setupTableView {
    
    //添加tableview
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.numberLabel.bottom).offset(50);
        make.leading.trailing.bottom.equalTo(self.view);
    }];
    
    self.tableView.backgroundColor = self.view.backgroundColor;
}

#pragma mark -- 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    
    if(indexPath.row == 0) {
        
        cell.imageView.image = [UIImage imageNamed:@"dianhua"];
        cell.textLabel.text = @"官服电话";
        cell.detailTextLabel.text = @"400-636-6113";
    }
    else {
        
        cell.imageView.image = [UIImage imageNamed:@"qq"];
        cell.textLabel.text = @"用户交流群";
        cell.detailTextLabel.text = @"QQ464768569";
    }
    //取消cell选中的颜色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

//点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(indexPath.row == 0) {
        
        //显示弹框
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否拨打电话：400-636-6113" preferredStyle:UIAlertControllerStyleAlert];
        //确定按钮
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://4006366113"]];
        }];
        //取消按钮
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        [alert addAction:cancelAction];
        [alert addAction:sureAction];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
}

//返回组头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *labelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, sWidth, 80)];
    
    labelView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:0.833];
    
    UILabel *label = [[UILabel alloc] init];
    
    [labelView addSubview:label];
    
    //约束
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(labelView);
        make.centerY.equalTo(labelView);
    }];
    
    label.text = @"欢迎联系我们";
    
    label.font = [UIFont systemFontOfSize:19];
    
    [label sizeToFit];
    
    label.textColor = [UIColor blackColor];
    
    return labelView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 40;
}

//点击屏幕，回退键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}

//懒加载
- (UITableView *)tableView {
    
    if(nil == _tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

@end
















